﻿# 元组
y = (1) # 整形
y0 = (1,) # 一元组
y1 = (12,23,34,45) # 四元组
y2 = ("姓名",12,"住址") # 三元组
print(y)
print(y0)
print(y1)
print(y2)
# 查看元组类型
print(type(y))
print(type(y0))
print(type(y1))
print(type(y2))
# 查看元组中元素数量
print(len(y1))
print(y2.__len__())
# 索引运算
print(y1[1])
print(y2[-1])
# 元组切片
print(y1[1:2])
print(y2[1:3:2])
# 遍历元组
for item in y1:
    print(item)
# 成员运算
print(12 in y1)
print("姓名" not in y2)
# 拼接运算
y3 = y1 + y2
print(y3)

# 比较运算
print(y1 == y3)
print(y1 <= y3)
print((y1 <= (12,23,34,45)))

a = ()
print(type(a))
b = ("hello")
print(type(b))
c = (100)
print(type(c))
d = ("hello",)
print(type(d))
e = (100,)
print(type(e))

# 元组打包与解压
f = 1,10,100
print(f)
print(type(f))
i,j,k = f
# i,j,k,l = f
print(i,j,k)

# 少数变量接受多个值
i,*j = f
print(i,j)
a,b,*c = range(10)
print(a,b,c)
a,b,c = [10,100,1000]
print(a,b,c)
a,*b,c = "hello"
print(a,b,c)

# 交换变量值
a = 1
b = "qwe"
c = True
print(a,b,c)
a,b,c = b,c,a
print(a,b,c)

# 元组与列表的对比
import timeit
print('%.3f秒'% timeit.timeit('[1,2,3,4,5,6,7,8,9]'))
print('%.3f秒'% timeit.timeit('(1,2,3,4,5,6,7,8,9)'))

# 元组列表相互转换
a = [1,2,3,4,5,6]
print(a)
print(tuple(a))
b = ("q","w","e","r","t")
print(b)
print(list(b))